<template>
  <div class="margin-container-list">
    <!-- 操作区域 -->
    <div class="tab-style-default">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="全部" name="0"></el-tab-pane>
        <el-tab-pane label="进行中" name="3"></el-tab-pane>
        <el-tab-pane label="已结束" name="4"></el-tab-pane>
        <el-tab-pane label="待审核" name="1"></el-tab-pane>
        <el-tab-pane label="待托管" name="2"></el-tab-pane>
      </el-tabs>
    </div>
    <div class="top-operate">
      <div>
        <el-input @keyup.enter.native="handleSearch" v-model="keyword" placeholder="请输入分发名称搜索"
                  style="width: 300px"></el-input>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </div>
    </div>
    <!-- 内容列表 -->
    <div class="table-container">
      <el-table :data="shareList" @sort-change="sortChange" @selection-change="handleSelectionChange"
                v-loading="listLoading" element-loading-text="加载中"
                style="width: 100%; align-content: center">
        <el-table-column width="240px" label="分发名称" prop="contentName">
        </el-table-column>
        <el-table-column width="240px" label="分发酬金" prop="amount">
        </el-table-column>
        <el-table-column width="240px" label="目标阅读量" prop="targetReading">
        </el-table-column>
        <el-table-column width="240px" label="当前阅读量" prop="readNum">
        </el-table-column>
        <el-table-column width="240px" label="当前转发量" prop="shareMemberNum">
        </el-table-column>
        <el-table-column width="240px" label="转发微客数" prop="shareNum">
        </el-table-column>
        <el-table-column width="240px" label="任务创建时间" prop="createTime" sortable="custom">
          <template slot-scope="scope">
            <span>{{scope.row.createTime}}</span>
          </template>
        </el-table-column>
        <el-table-column width="240px" label="状态" prop="status">
          <template slot-scope="scope">
            <template v-if="scope.row.status === 21">
              待开始
            </template>
            <template v-else>
              <span :type="scope.row.status | statusFilter">{{ statuss[scope.row.status] }}</span>
            </template>
          </template>
        </el-table-column>
        <el-table-column width="140px" label="操作" align="right" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="medium" @click="handleUpdate(scope.row)">查看</el-button>
            <template v-if="scope.row.status == '3'">
              <el-button type="text" size="medium" @click="buttonClickStop(scope.row)">中止</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 列表分页 -->
    <div class="pagination">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="shareListQuery.currentPage"
                     :page-sizes="[10, 20, 50, 100]" :page-size="shareListQuery.size"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>
  </div>
</template>


<style scoped>
  .user-avatar{
    width: 46px;
    height: 46px;
    display: block;
    border-radius: 50px;
  }
</style>

<script>
  import { getNameByPath } from '@/utils/get-name-by-path' // 不要删除，根据URL获取文件名
  import { pagingShare, checkContentArticle, endShare } from '@/api/content/share'
  import elDragDialog from '@/directive/el-dragDialog' // base on element-ui

  export default {
    name: getNameByPath(),
    directives: {
      // 拖动窗口
      elDragDialog
    },
    data() {
      return {
        shareList: [],
        statuss: {},
        activeName: '0',
        total: null,
        listLoading: true,
        keyword: '',
        multipleSelection: [],
        shareListQuery: {
          currentPage: 1,
          size: 10,
          ascs: '',
          descs: 'createTime',
          startDate: null,
          endDate: null,
          status: '',
          contentName: null,
          enterpriseId: this.getEnterpriseId()
        },
        share: {
          contentId: '',
          contentCode: '',
          contentName: '',
          targetReading: '',
          createTime: '',
          updateTime: '',
          status: '',
          contentArticle_id: '',
          amount: '',
          startTime: '',
          endTime: '',
          enterpriseId: '',
          delFlag: '',
          contentShelf: ''
        }
      }
    },
    // 过滤器
    filters: {
      statusFilter(status) {
        const statusMap = {
          1: 'success',
          2: 'danger'
        }
        return statusMap[status]
      }
    },
    // 初始化页面
    created() {
      this.initShareList()
    },
    methods: {
      initShareList() {
        this.listLoading = true
        pagingShare(this.shareListQuery).then(response => {
          if (response.isError) return
          this.total = response.data.total
          this.shareList = response.data.records
          if (response.data.dict) {
            this.statuss = response.data.dict.status
          }
          this.listLoading = false
        })
      },
      // 列表排序
      sortChange(data) {
        const { prop, order } = data
        if (prop === 'createTime') {
          if (order === 'ascending') {
            this.shareListQuery.ascs = 'createTime'
            this.shareListQuery.descs = ''
          } else {
            this.shareListQuery.ascs = ''
            this.shareListQuery.descs = 'createTime'
          }
        }
        this.initShareList()
      },
      handleClick() {
        if (this.activeName !== '0') {
          this.shareListQuery.status = this.activeName
        } else {
          this.shareListQuery.status = ''
        }
        this.initShareList()
      },
      handleSearch() {
        this.searchReset()
        this.shareListQuery.currentPage = 1
        if (this.starEndTime) {
          if (this.starEndTime.length > 0) {
            this.shareListQuery.startDate = this.starEndTime[0]
            this.shareListQuery.endDate = this.starEndTime[1]
          }
        }
        if (this.keyword !== '') {
          this.shareListQuery.contentName = this.keyword
        }
        this.shareListQuery.authFlag = this.authFlagType
        this.initShareList()
      },
      searchReset() {
        this.shareListQuery = {
          currentPage: 1,
          size: 10,
          startDate: null,
          endDate: null,
          status: '',
          contentName: null,
          enterpriseId: this.getEnterpriseId()
        }
      },
      handleSizeChange(val) {
        this.shareListQuery.size = val
        this.initShareList()
      },
      handleCurrentChange(val) {
        this.shareListQuery.currentPage = val
        this.initShareList()
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      // 新建分发
      handleCreate() {
        checkContentArticle(this.getEnterpriseId()).then(response => {
          if (response.isError) return
          if (response.data === false) {
            this.$confirm('没有文章可以分发, 是否去新增文章?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              // 跳新增文章界面
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消'
              })
            })
          } else {
            // 跳分发页面
          }
        })
      },
      // 详情
      handleUpdate(row) {
        this.$router.push({ path: '/content/share/detail/index', query: { contentId: row.contentId }})
      },
      // 结束项目
      buttonClickStop(row) {
        this.$confirm('是否中止项目?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 更改任务状态
          endShare(row.contentId).then(response => {
            if (response.isError) return
            this.$message({
              type: 'success',
              message: '成功!'
            })
            this.initShareList()
          })
        }).catch(() => {
        })
      }
    },
    mounted() {

    }
  }
</script>
